<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/loading.js"></script>
    <link href="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/theme-chalk/index.css" rel="stylesheet">
    <link href="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/treeTable.css" rel="stylesheet">


<body>

<div id="app">
    <el-container>
        <el-header>
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">基础库</el-breadcrumb-item>
                <el-breadcrumb-item>分类管理库</el-breadcrumb-item>
            </el-breadcrumb>
        </el-header>
        <el-header>

            <el-row type="flex" class="row-bg" justify="space-between">
                <el-col :span="2">
                    <el-button type="primary" icon="el-icon-edit" :offset="6" @click="add">新增</el-button>
                </el-col>
            </el-row>
        </el-header>
        <tree-table :data="dataTree" :eval-func="func" :eval-args="args"  border size="mini" height="calc(100vh - 200px)"
                    style="margin-left:1%;width:98%;overflow:auto">
            <#--<el-table-column label="类别名称" align="center">-->
                <#--<template slot-scope="scope">-->
                        <#--<span style="color:#606266;font-size: 14px"></i>{{-->
                            <#--scope.row.label }}</span>-->
                <#--</template>-->
            <#--</el-table-column>-->
            <el-table-column label="操作" align="center" min-width="100">
                <template slot-scope="scope">
                    <el-button type="text" size="mini" @click="append(scope.row)">添加</el-button>
                    <el-button type="text" size="mini" @click="modify(scope.row)">编辑</el-button>
                    <el-button type="text" size="mini" @click="remove(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </tree-table>
        <el-dialog title="请确认分类信息" :visible.sync="dialogAppendVisible">
            <el-form label-width="20%">
                <el-row>
                    <el-col :xs="24" :sm="24" :md="24" :lg="16" :xl="12">
                <el-form-item label="分类名称">
                    <el-input v-model="label" autoComplete="off"></el-input>
                </el-form-item>
                        </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAppendVisible = false">取 消</el-button>
                <el-button type="primary" @click="registerLogin">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 修改资源模态框 -->
        <el-dialog title="请确认分类信息" :visible.sync="dialogModifyVisible">
            <el-form label-width="20%">
                <el-row>
                    <el-col :xs="24" :sm="24" :md="24" :lg="16" :xl="12">
                <el-form-item label="分类名称">
                    <el-input v-model="label" autocomplete="off"></el-input>
                </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogModifyVisible = false">取 消</el-button>
                <el-button type="primary" @click="newly">确 定</el-button>
            </div>
        </el-dialog>
    </el-container>
</div>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/dist/vue.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/index.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/treeTable.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/publiccss/assets/global/plugins/jquery.min.js" type="text/javascript"></script>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                func: null,
                expandAll: false,
                args: [null, null, 'timeLine'],
                dataTree: [],
                dialogAppendVisible: false,
                dialogModifyVisible: false,
                id: '',
                label: '',
                tempNode: {},
            }
        },
        methods: {
            add() {
                this.id = '';
                this.label = '';
                this.tempNode = {};
                this.dialogAppendVisible = true;
            },
            //打开弹窗 获取当前修改的节点信息放入缓存
            modify(val) {
                this.dialogModifyVisible = true;
                //当前修改节点ID
                this.id = val.id;
                //当前修改参数1，重新赋值
                this.label = val.label;

            },
            //提交当前修改的节点信息
            newly() {
                //发送请求
                var updateBool = false;
                var id = this.id;
                var name = this.label;
                $.ajax({
                    url: '${url}/pzCfgCataLogController/treeUpdate.json',
                    type: 'get',
                    data: { id: id, name: name },
                    async: false,
                    dataType: 'json',
                    success: function (result) {
                        updateBool = result.ret;
                    }
                });
                if (updateBool) {
                    this.$message({
                        type: 'success',
                        message: '修改成功!'
                    });
                    this.id = '';
                    this.label = '';
                    //关闭弹窗
                    this.dialogModifyVisible = false;
                    this.ajaxFunction();
                } else {
                    this.$message({
                        type: 'info',
                        message: '数据修改异常!'
                    });
                }
            },
            //删除当前节点信息
            remove(val) {
                this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                var bool = false;
                $.ajax({
                    url: '${url}/pzCfgCataLogController/treeDel.json',
                    type: 'get',
                    data: { id: val.id },
                    async: false,
                    dataType: 'json',
                    success: function (result) {
                        bool = result.ret;
                    }
                });
                if (bool) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.ajaxFunction();
                } else {
                    this.$message({
                        type: 'info',
                        message: '删除失败'
                    });
                }
            }).catch(() => {
                    this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

            },
            //添加节点  打开弹窗 清空缓存的节点信息，并缓存当前添加的父节点信息
            append(val) {
                this.label = '';
                this.dialogAppendVisible = true;
                this.tempNode = val;
            },
            //添加节点信息，发送请求
            registerLogin() {
                const newChild = {
                    parent_id: this.tempNode.id,
                    //名称
                    name: this.label,
                    //子节点默认空
                    children: []
                };
                const addChild = {
                    //ID赋值
                    // id: id++,
                    id: this.tempNode.id,
                    //名称
                    text: this.label,
                    //子节点默认空
                    children: []
                };
                //发送请求
                var bool = true;
                var dataId = 0;
                $.ajax({
                    url: '${url}/pzCfgCataLogController/pzCfgCataLogTreeAdd.json',
                    type: 'post',
                    data: newChild,
                    async: false,
                    dataType: 'json',
                    success: function (result) {
                        bool = result.ret;
                        dataId = result.data;
                    }
                });
                if (bool) {
                    if (!this.tempNode.children) {
                        this.$set(this.tempNode, 'children', []);
                    }
                    //存值
                    addChild.id = dataId;
                    this.tempNode.children.push(addChild);
                    // 关闭弹窗
                    this.dialogAppendVisible = false;
                    this.label = '';
                    this.ajaxFunction();
                    this.$message({
                        type: 'success',
                        message: '添加成功!'
                    });
                } else {
                    this.$message({
                        type: 'info',
                        message: '数据添加异常!'
                    });
                }
            },
            ajaxFunction() {
                var resultData = this.dataTree;
                var boolean = false;
                $.ajax({
                    url: '${url}/pzCfgCataLogController/pzCfgCataLogTree.json',
                    type: 'get',
                    async: false,
                    success: function (result) {
                        boolean=result.ret;
                        if (boolean) {
                            resultData = result.data;
                        }
                    }
                });
                if(boolean){
                    this.dataTree = resultData;
                }else{
                    this.$message.error('请求异常');
                }

            },
        },
        mounted() {
            this.ajaxFunction();
        },

    })
</script>
</body>

</html>